﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
    /// <summary>
    /// 同步销售收款
    /// </summary>
    public class A08 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {

            E01A08P P = (E01A08P)handler.Serial.Deserialize(st, typeof(E01A08P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[7];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@BuildDate", SqlDbType.DateTime);
                sps[2].Value = Convert.ToDateTime(P.BuildDate);
                sps[3] = new SqlParameter("@CusName", SqlDbType.NVarChar, 50);
                sps[3].Value = P.CusName;
                sps[4] = new SqlParameter("@PayAmount", SqlDbType.Decimal);
                sps[4].Value = P.PayAmount;
                sps[5] = new SqlParameter("@IsEnabled", SqlDbType.Bit);
                sps[5].Value = P.IsEnabled;
                sps[6] = new SqlParameter("@Remark", SqlDbType.NVarChar, 50);
                sps[6].Value = P.Remark;

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @FID INT\r\n");
                sql.Append("DECLARE @CusNameFID INT\r\n SET @CusNameFID = IsNull((SELECT FItemID FROM t_Organization WHERE FHomePage =@CusName AND @CusName<>''),0)\r\n");
                sql.Append("DECLARE @Count INT\r\n SET @Count=IsNull((SELECT 1 FROM KEW_SalesCash WHERE FBillNo=@Number),0)");
                sql.Append("IF @Count>0 BEGIN\r\n");
                sql.Append("  UPDATE KEW_SalesCash SET FBase=@CusNameFID,FDate=@BuildDate,FDecimal=@PayAmount,FNOTE=@Remark,FCheckBox1=@IsEnabled WHERE FBillNo=@Number\r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END \r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_SalesCash',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_SalesCash ([FClassTypeID],[FID],[FBillNo],[FBase],[FDate],[FCheckBox1],[FDecimal],[FBiller],[FBase1],[FNOTE],[FUser],[FText]) ");
                sql.Append("  VALUES (236900149,@FID,@Number,@CusNameFID,@BuildDate,@IsEnabled,@PayAmount,0,0,@Remark,0,@ID)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
               
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("销售收款同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售收款同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售收款同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 销售收款
    /// </summary>
    public class E01A08P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        //public String Name;
        /// <summary>
        /// 建档日期
        /// </summary>
        public String BuildDate;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CusName;
        /// <summary>
        /// 付款金额
        /// </summary>
        public Decimal PayAmount;
        /// <summary>
        /// 生效
        /// </summary>
        public Boolean IsEnabled;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        

    }
}
